package com.lianliankan.llkan;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.llkan.R;
import com.lianliankan.util.ButtonSelector;
import com.lianliankan.util.ExtendMethod;
import com.lianliankan.util.GlobalValue;
import com.lianliankan.util.SettlementAsyncTask;

public class StagePropertyActivity extends Activity {
	private ImageButton gameStartButton;
	private TextView liveText;
	private TextView moneyText;
	private TextView dimondText;
	private LinearLayout sply;
	private int[] drawableIds = { R.drawable.sp1, R.drawable.sp2,
			R.drawable.sp3, R.drawable.sp4, R.drawable.sp5 };
	private ExtendMethod extendMethod;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_stageproperty);
		findView();
		init();
	}

	private void findView() {
		sply = (LinearLayout) findViewById(R.id.sply);
		liveText = (TextView) findViewById(R.id.splive);
		moneyText = (TextView) findViewById(R.id.spmoney);
		dimondText = (TextView) findViewById(R.id.spdimond);
	}

	private void init() {
		ButtonSelector bSelector = new ButtonSelector();

		handler.post(checkUserThread);
		StagePropertyView spv = new StagePropertyView(this, null);
		sply.addView(spv);

		gameStartButton = (ImageButton) findViewById(R.id.gameStartButton);
		gameStartButton.setBackgroundDrawable(bSelector.newSelector(
				StagePropertyActivity.this, R.drawable.gamebigin,
				R.drawable.gamebiginpress, R.drawable.gamebiginpress,
				R.drawable.gamebiginpress));
		gameStartButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (GlobalValue.currentUser.getBenevolence() < 1) {
					AlertDialog.Builder alert = new AlertDialog.Builder(
							StagePropertyActivity.this);
					alert.setTitle("爱心不足！");
					alert.show();
				} else {
					SettlementAsyncTask settlementAsyncTask = new SettlementAsyncTask(
							null, "begin");
					settlementAsyncTask.execute();
					Intent intent = new Intent(StagePropertyActivity.this,
							GameActivity.class);
					startActivity(intent);
					finish();
				}
			}
		});
	}

	Handler handler = new Handler();
	Runnable checkUserThread = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			if (GlobalValue.currentUser != null) {
				liveText.setText(String.valueOf(GlobalValue.currentUser
						.getBenevolence()));
				moneyText.setText(String.valueOf(GlobalValue.currentUser
						.getMoney()));
				dimondText.setText(String.valueOf(GlobalValue.currentUser
						.getDiamond()));
			} else {
				handler.postDelayed(checkUserThread, 1000);
			}

		}
	};

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(StagePropertyActivity.this,
					RankActivity.class);
			startActivity(intent);
			finish();
		}
		return false;
	}

	protected void onResume() {
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onResume();
	}
}
